<?php
	require("libraries/student.php");
	class Answers extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_answer");
			$this->load->library("string");
			$this->load->library("form_validation");
			$this->load->database();
		}
		public function index(){
			$config['base_url'] = base_url().'admin/answers/index/';
			$config['total_rows'] = $this->model_answer->countall();
			$data['title'] = "Answers";
			$data['template'] = "answers/list";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";$data['act'] = 5;
			$data['info']= $this->model_answer->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['data']  = "";$data['act'] = 5;
			$data['title'] = "Add new question";
			$data['template'] = "answers/add";
			if($this->input->post("okie") != ""){
				$title = trim($this->input->post('atitle'));
				$url = str_replace(' ','-',strtolower($this->string->replace($title)));
				$db = array(
					"atitle" => $this->input->post("atitle"),
					"ainfo" => $this->input->post("ainfo"),
					"alike" => $this->input->post("alike"),
					"adislike" => $this->input->post("adislike"),
					"adate" => date('d/m/Y-H:i:s'),
					"userid" => $this->input->post("userid"),
					"quesid" => $this->input->post("quesid")
				);
				$this->model_answer->add($db);
				redirect(base_url()."admin/answers");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['error'] = "";$data['act'] = 5;
			$data['title'] = "Edit a question";
			$data['template'] = "answers/edit";
			$data['result'] = $this->model_answer->getdata($id);
			$data['data'] = "vl";
			if($this->input->post("okie") != ""){
				$title = trim($this->input->post('title'));
				$url = str_replace(' ', '-',strtolower($this->string->replace($title)));
				$db = array(
					"atitle" => $this->input->post("atitle"),
					"ainfo" => $this->input->post("ainfo"),
					"alike" => $this->input->post("alike"),
					"adislike" => $this->input->post("adislike"),
					"astatus" => $this->input->post("astatus"),
					"adate" => date('d/m/Y-H:i:s'),
					"userid" => $this->input->post("userid"),
					"quesid" => $this->input->post("quesid")
				);
				$this->model_answer->update($id,$db);
				redirect(base_url()."admin/answers");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function getcago(){
			$id = $_POST['id'];
			$data = $this->model_answer->listcago($id);
			if($data != NULL){
				echo '<select name="cagoid">';
                foreach($data as $items){
					echo "<option value='".$items['id']."'>".$items['name']."</option>";
				}
                echo "</select>";
			}else{
				echo "No data";
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_answer->getdata($id);
			//@unlink("uploads/answers/".$data['image']);
			//@unlink("uploads/answers/thumb/".$data['image']);
			$this->model_answer->del($id);
			redirect(base_url()."admin/answers");
		}
		public function createThumbnail($fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/answers/'.$fileName;
			$config['new_image'] = 'uploads/answers/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}